package com.GLM.Viewer.Users;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GLM.Constants.Constant;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;


public class UserSettingLocationActivity extends Activity{
	private final static int LOCATIONLIST_LOADING = 111;
	private final static int LOCATIONLIST_LOADED = 222;
	private ListView mProvinceListView;
	private ListView mCityListView;
	private ListView mCountyListView;
	private SimpleAdapter mAdapter;
	private ProgressBar mProgressBar;
	private int locationStyle = -1;
	private int root_id;
	private String locName="";
	private int loc_id;
	private int acc_loc_id;
	private String[] locItemKeys = new String[] { "locName" };
	private int[] locItemViews = new int[] { R.id.userdetail_location_itemText};
	private List<Map<String, Object>> mLocationList = new ArrayList<Map<String,Object>>();
	private Map<Integer, String> tempMap = new HashMap<Integer, String>();
	private Map<String, Integer> locationMap = new HashMap<String, Integer>();
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			Thread thread;
			switch (msg.what) {
			case LOCATIONLIST_LOADING:
				thread = new Thread(new Runnable() {

					public void run() {
						// TODO Auto-generated method stub						
						initLocationData();
						mAdapter = new SimpleAdapter(UserSettingLocationActivity.this, mLocationList, R.layout.view_userdetail_location_item, locItemKeys, locItemViews);
						mHandler.sendEmptyMessage(LOCATIONLIST_LOADED);
					}
				});
				thread.start();
				break;
			case LOCATIONLIST_LOADED:
				mProgressBar.setVisibility(View.INVISIBLE);
				if(locationStyle == 2 ){
					
					mCityListView.setAdapter(mAdapter);
				}else if(locationStyle == 3){
					mCountyListView.setAdapter(mAdapter);
				}else{
					mProvinceListView.setAdapter(mAdapter);
				}
				
				bindItemClick();
				break;

			default:
				break;
			}
		
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_userdetail_location);
		mProvinceListView = (ListView)findViewById(R.id.location_province_list);
		mCityListView = (ListView)findViewById(R.id.location_city_list);
		mCountyListView = (ListView)findViewById(R.id.location_county_list);
		mProgressBar = (ProgressBar)findViewById(R.id.location_progressbar);
		mProvinceListView.setClickable(true);
		mHandler.sendEmptyMessage(LOCATIONLIST_LOADING);
	}
	
	
	public void bindItemClick(){
		mProvinceListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
			
				String provinceName = tempMap.get(position);
				root_id = locationMap.get(provinceName);
				Log.d("clickid", root_id+"");
				mProvinceListView.setVisibility(View.INVISIBLE);
				mCityListView.setVisibility(View.VISIBLE);
				tempMap.clear();
				locationStyle = 2;
				locationMap.clear();
				mLocationList.clear();
				mHandler.sendEmptyMessage(LOCATIONLIST_LOADING);
				
			}
		});
		
		mCityListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String cityName = tempMap.get(position);
				root_id = locationMap.get(cityName);
				loc_id = root_id;
				Log.d("clickid", root_id+"");
				locName += cityName;
				mCityListView.setVisibility(View.INVISIBLE);
				mCountyListView.setVisibility(View.VISIBLE);
				tempMap.clear();
				locationStyle = 3;
				locationMap.clear();
				mLocationList.clear();
				mHandler.sendEmptyMessage(LOCATIONLIST_LOADING);
			}
		});
		
		mCountyListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				String countryName = tempMap.get(position);
				root_id = locationMap.get(countryName);
				acc_loc_id = root_id;
				Log.d("clickid", root_id+"");
				locName += " ";
				locName += countryName;
				tempMap.clear();
				locationMap.clear();
				mLocationList.clear();
				Intent intent = new Intent(UserSettingLocationActivity.this, UserSettingActivity.class);
				intent.putExtra("locName", locName);
				intent.putExtra("loc_id", loc_id);
				intent.putExtra("acc_loc_id", acc_loc_id);
				
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
	
	public void initLocationData(){
		JSONObject jsonData = new JSONObject();
		JSONResponse jsonResponse = null;
		try {
			if (locationStyle == 2 || locationStyle == 3) {
				jsonData.put("root_id", root_id);
				jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_LOCATION_CITY, jsonData);
			}
			else{
				jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_LOCATION_PROVINCE, jsonData);
			}
			
			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					JSONArray jsonArray = data.getJSONArray(Constant.RESP_LOCDATA);
					initLocationPlaceFromJson(jsonArray);
				} 

			}
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
	}
	
	public void initLocationPlaceFromJson(JSONArray jsonArray) throws JsonSyntaxException, JSONException {
		Gson gson = new Gson();
		for (int i = 0; i < jsonArray.length(); i++) {
			LocationEntity entivity = gson.fromJson(jsonArray.getString(i).toString(), LocationEntity.class);
			Map<String, Object> item = new HashMap<String, Object>();
			Log.d("SettingLocationData", entivity.getLoc_name()+">>"+entivity.getLoc_id());
			item.put("locName", entivity.getLoc_name());
			
			tempMap.put(i, entivity.getLoc_name());
			locationMap.put(entivity.getLoc_name(), entivity.getLoc_id());
			mLocationList.add(item);
		}

	}
	
	public class LocationEntity{
		private String loc_name;
		private int loc_id;
		
		public String getLoc_name() {
			return loc_name;
		}
		public void setLoc_name(String loc_name) {
			this.loc_name = loc_name;
		}
		public int getLoc_id() {
			return loc_id;
		}
		public void setLoc_id(int loc_id) {
			this.loc_id = loc_id;
		}
		
		
	}
}

